package com.headStorm.hutooluse;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;

/**
 * @author zhangqq
 * @create 2021-12-13 19:50
 **/
public class HutoolCacheUse {

    public static void main(String[] args) {
        TimedCache<String, String> timedCache = CacheUtil.newTimedCache(1000);
        timedCache.put("1", "aa");
        timedCache.put("2", "bbb");


        ExecutorService executorService = ThreadUtil.newExecutor(1);
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                 while (true){
                     ThreadUtil.safeSleep(100);
                     timedCache.put(new Date().toString(), new Date().toString());
                 }
            }
        });

        while (true){
            timedCache.forEach(new Consumer<String>() {
                @Override
                public void accept(String s) {
                    System.out.println(s);
                }
            });
        }
    }
}
